package com.bum.guru.keeng.bo;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bum.guru.exp.keeng.om.KeengAlbum;
import com.bum.guru.exp.keeng.om.KeengSong;
import com.bum.guru.exp.keeng.om.PlayListItem;
import com.bum.guru.util.BaseParser;

public class KeengParser {
	Map<String, KeengAlbum>			cacheAlbum		= new HashMap<String, KeengAlbum>();
	Map<String, List<PlayListItem>>	cachePlaylist	= new HashMap<String, List<PlayListItem>>();

	private KeengAlbum fetchAlbum(String code) {
		String url = "http://www.keeng.vn/album/a/" + code + ".html";
		String html = BaseParser.loadHtml(url);
		html = html.replace("\"", "`");
		html = BaseParser.parseContent(html, "<div class=`box-player box-player-album`>", "<!--end-box-lyric-->");

		System.out.println(html);
		String cover = BaseParser.parseContent(html, "<img src=`", "`>");
		String title = BaseParser.parseContent(html, "<h3>", "</h3>");
		String artist = BaseParser.parseContent(title, "`>", "</a>");
		if (artist != null) {
			title = BaseParser.parseContent(title, null, " - <a");
		}
		
		String cat = BaseParser.parseContent(html, "Thể loại: <a class=`` title=`", "`");

		List<KeengSong> songs = new LinkedList<KeengSong>();

		String part = BaseParser.parseContent(html, "<div class=`list-play-album`>", "<div class=`player-service`>").trim();
		for (String row : StringUtils.splitByWholeSeparator(part, "<div class=`play-album-row`>")) {
			String songId = BaseParser.parseContent(row, "<a playingId=`", "`");
			String songTitle = BaseParser.parseContent(row, "title=`", "`");
			String songArtist = BaseParser.parseContent(row, "<a title=`", "`");

			KeengSong song = new KeengSong();
			song.setAlbumId(code);
			song.setId(songId);
			song.setArtist(songArtist);
			song.setTitle(songTitle);

			songs.add(song);
		}

		part = BaseParser.parseContent(html, "<div id=`change_content0` class=`lyric-content info-content hide-content`>", null).replace("`", "\"").trim();
		String[] parts = StringUtils.splitByWholeSeparator(part, "class=\"lyric-content info-content hide-content\">");
		for (int i = 0; i < parts.length; i++) {
			String row = parts[i];
			row = BaseParser.parseContent(row, "\">", "<a id=\"input_id");
			if (row == null) {
				break;
			}
			row = BaseParser.cleanAttribute(row).replace("<br />", "<br>").replaceAll("(<br>|</p>)", "\n").trim();
			row = BaseParser.removeHtmlTag(row).trim();

			KeengSong song = songs.get(i);
			song.setLyric(row);
		}

//		url = "http://www.keeng.vn/kusers.php/album/" + code + ".xml";
//		html = BaseParser.loadHtml(url);
//		html = BaseParser.parseContent(html, "<track>", "</playlist>");
//		parts = StringUtils.splitByWholeSeparator(html, "<track>");
//		for (int i = 0; i < parts.length; i++) {
//			String stream = BaseParser.parseContent(parts[i], "<location>", "</location>");
//			KeengSong song = songs.get(i);
//			song.setStream(stream);
//		}

		KeengAlbum album = new KeengAlbum();
		album.setId(code);
		album.setTitle(title);
		album.setArtist(artist);
		album.setCover(cover);
		album.setCat(cat);
		album.setSongs(songs);

		return album;
	}

	private List<PlayListItem> fetchAlbumPlaylist(String code) {
		String url = "http://www.keeng.vn/kusers.php/album/" + code + ".xml";
		String html = BaseParser.loadHtml(url);
		html = BaseParser.parseContent(html, "<track>", "</playlist>");
		String[] parts = StringUtils.splitByWholeSeparator(html, "<track>");

		List<PlayListItem> items = new LinkedList<PlayListItem>();
		for (int i = 0; i < parts.length; i++) {
			String title = BaseParser.parseContent(parts[i], "<title>", "</title>");
			String stream = BaseParser.parseContent(parts[i], "<location>", "</location>");

			items.add(new PlayListItem(title, stream));
		}
		return items;
	}

	public KeengAlbum getAlbum(String code) {
		if (cacheAlbum.containsKey(code)) {
			return cacheAlbum.get(code);
		}
		KeengAlbum album = fetchAlbum(code);
		cacheAlbum.put(code, album);

		return album;
	}

	public List<PlayListItem> getAlbumPlaylist(String code) {
		if (cachePlaylist.containsKey(code)) {
			return cachePlaylist.get(code);
		}

		List<PlayListItem> items = fetchAlbumPlaylist(code);
		cachePlaylist.put(code, items);

		return items;
	}

	public static void main(String[] args) {
		KeengParser k = new KeengParser();
		k.fetchAlbum("6F8446F2");
	}
}
